#include "mysql_friend_apply.hpp"
#include "mysql_chat_session.hpp"
#include "mysql_chat_session_member.hpp"
#include "mysql_friend_relation.hpp"
#include <gflags/gflags.h>

DEFINE_bool(run_mode, false, "程序的运行模式，false-调试；true-发布");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

void relation_insert_test(ChatSystem::RelationTable& relation)
{
    relation.insert("uid1", "uid2");
    relation.insert("uid1", "uid3");
    relation.insert("uid2", "uid3");
}
void relation_select_test(ChatSystem::RelationTable& relation)
{
    auto res = relation.friends("uid1");
    for(const auto& id : res)
    {
        std::cout << id << std::endl;
    }
}

void relation_remove_test(ChatSystem::RelationTable& relation)
{
    if(relation.exists("uid1", "uid3"))
    {
        relation.remove("uid1", "uid3");
    }
    if(relation.exists("uid1", "uid2"))
    {
        relation.remove("uid1", "uid2");
    }
}

void apply_insert_test(ChatSystem::FriendApplyTable& friend_apply)
{
    ChatSystem::FriendApply friend_apply1("event_id1", "uid2", "uid1");
    ChatSystem::FriendApply friend_apply2("event_id2", "uid3", "uid1"); 
    ChatSystem::FriendApply friend_apply3("event_id3", "uid3", "uid2"); 
    ChatSystem::FriendApply friend_apply4("event_id4", "uid4", "uid1"); 
    ChatSystem::FriendApply friend_apply5("event_id5", "uid5", "uid1"); 

    friend_apply.insert(friend_apply1);
    friend_apply.insert(friend_apply2);
    friend_apply.insert(friend_apply3);
    friend_apply.insert(friend_apply4);
    friend_apply.insert(friend_apply5);

}
void apply_select_test(ChatSystem::FriendApplyTable& friend_apply)
{
    auto res = friend_apply.applyUsers("uid1");
    for(const auto& id : res)
    {
        std::cout << id << std::endl;
    }
}

void apply_remove_test(ChatSystem::FriendApplyTable& friend_apply)
{
    friend_apply.remove("event_id1");
    friend_apply.remove("uid5", "uid1");
}

void apply_exists_test(ChatSystem::FriendApplyTable& friend_apply)
{
    bool ret = friend_apply.exists("uid3", "uid1");
    std::cout << ret << std::endl;
}


void chat_session_insert_test(ChatSystem::ChatSessionTable& chat_session)
{
    ChatSystem::ChatSession chat_session1("ssid1", "csn1", ChatSystem::ChatSessionType::SINGLE);
    ChatSystem::ChatSession chat_session2("ssid2", "csn2", ChatSystem::ChatSessionType::SINGLE); 
    ChatSystem::ChatSession chat_session3("ssid3", "csn3", ChatSystem::ChatSessionType::SINGLE); 
    ChatSystem::ChatSession chat_session4("ssid4", "csn4", ChatSystem::ChatSessionType::GROUP); 
    ChatSystem::ChatSession chat_session5("ssid5", "csn5", ChatSystem::ChatSessionType::GROUP); 

    chat_session.insert(chat_session1);
    chat_session.insert(chat_session2);
    chat_session.insert(chat_session3);
    chat_session.insert(chat_session4);
    chat_session.insert(chat_session5);

}
void chat_session_select_test(ChatSystem::ChatSessionTable& chat_session)
{
    auto res = chat_session.select("ssid1");
    std::cout << res->chat_session_id() << std::endl;
    std::cout << res->chat_session_name() << std::endl;
    std::cout << (int)res->chat_session_type() << std::endl;
}

void chat_session_remove_test(ChatSystem::ChatSessionTable& chat_session)
{
    // chat_session.remove("ssid1");
    chat_session.remove("uid6", "uid7");
}

void chat_session_single_test(ChatSystem::ChatSessionTable& chat_session)
{
    auto res = chat_session.singleChatSession("uid1");
    for(const auto& a : res)
    {
        std::cout << a.chat_session_id << std::endl;
        std::cout << a.friend_id << std::endl;

    }
}

void chat_session_group_test(ChatSystem::ChatSessionTable& chat_session)
{
    auto res = chat_session.groupChatSession("uid2");
    for(const auto& a : res)
    {
        std::cout << a.chat_session_id << std::endl;
        std::cout << a.chat_session_name << std::endl;

    }
}


int main(int argc, char* argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    ChatSystem::InitLogger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    auto db = ChatSystem::ODBFactory::create("root", "zhi204071", "127.0.0.1", "chatsystem", "utf8", 0, 1);
    ChatSystem::RelationTable relation(db);
    ChatSystem::ChatSessionTable chat_session(db);
    ChatSystem::FriendApplyTable friend_apply(db);

    // relation_insert_test(relation);

    // relation_select_test(relation);

    // relation_remove_test(relation);

    // apply_insert_test(friend_apply);

    // apply_select_test(friend_apply);

    // apply_remove_test(friend_apply);

    // apply_exists_test(friend_apply);

    // chat_session_insert_test(chat_session);

    // chat_session_select_test(chat_session);

    // chat_session_single_test(chat_session);
    // std::cout << "--------------------" << std::endl;
    // chat_session_group_test(chat_session);

    chat_session_remove_test(chat_session);

    

    return 0;
}
